package com.ruoyi.scheme.service.Impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.schemeEnum.SchemeException;
import com.ruoyi.scheme.dto.EcologicalRepairSchemeDTO;
import com.ruoyi.scheme.mapper.EcologicalRepairSchemeMapper;
import com.ruoyi.scheme.service.EcologicalRepairSchemeService;
import com.ruoyi.scheme.vo.EcologicalRepairSchemeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EcologicalRepairSchemeServiceImpl implements EcologicalRepairSchemeService {

    @Autowired
    private EcologicalRepairSchemeMapper ecologicalRepairSchemeMapper;


    //创建生态修复方案
    @Override
    @DataSource(DataSourceType.SLAVE)
    public void create(EcologicalRepairSchemeDTO ecologicalRepairSchemeDTO) {
        ecologicalRepairSchemeMapper.insert(ecologicalRepairSchemeDTO);
    }

    //删除生态修复方案
    @Override
    @DataSource(DataSourceType.SLAVE)
    public void delete(Long schemeId) {
      if(ecologicalRepairSchemeMapper.selectById(schemeId) == null){
          throw new ServiceException(SchemeException.SCHEME_NOT_EXIST);
      }
        ecologicalRepairSchemeMapper.delete(schemeId);
    }

    //修改生态修复方案
    @Override
    @DataSource(DataSourceType.SLAVE)
    public void update(EcologicalRepairSchemeDTO ecologicalRepairSchemeDTO) {
        if(ecologicalRepairSchemeMapper.selectById(ecologicalRepairSchemeDTO.getSchemeId()) == null){
            throw new ServiceException(SchemeException.SCHEME_NOT_EXIST);
        }
        ecologicalRepairSchemeMapper.update(ecologicalRepairSchemeDTO);
    }

    //查询生态修复方案
    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<EcologicalRepairSchemeVO> search(Long schemeId, Integer patchId) {
        List <EcologicalRepairSchemeVO> list=ecologicalRepairSchemeMapper.select(schemeId,patchId);
        if(list==null|| list.isEmpty()){
            throw new ServiceException(SchemeException.SCHEME_NOT_EXIST);
        }
        return list;
    }


}
